package com.example.testdemo.demos.web.Controller;


import com.example.testdemo.demos.web.Config.JwtInfo;
import com.example.testdemo.demos.web.Config.Result;
import com.example.testdemo.demos.web.Config.ResultEnum;
import com.example.testdemo.demos.web.Entity.Merchant;
import com.example.testdemo.demos.web.Service.JwtRedistService;
import com.example.testdemo.demos.web.Service.MerchantService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
@RestController
@RequestMapping("/merchant")
public class MerchantController {
  @Resource
  private MerchantService merchantService;
  @Resource
  private JwtRedistService jwtRedistService;

  /**
   * 新增商家
   * @param merchant
   * @param token
   * @return
   */
  @PostMapping("/addMerchant")
  public Result addMerchant(@RequestBody Merchant merchant, @RequestHeader("token") String token) {
    Result result = new Result();
    try {
      JwtInfo jwtInfo = new JwtInfo();
      if (!(token.isEmpty())) {
        if (!(merchant.getLegalPerson() ==null)){
          jwtInfo = jwtRedistService.getUserInfo(token);
          return merchantService.addMerchant(merchant, jwtInfo);
        }else {
          result.setCode(ResultEnum.BADREQUEST.getCode());
          result.setMsg("请求参数不足或不合法！");
        }
      } else {
        jwtInfo.setPass(false);
      }
    } catch (Exception e) {
      result.setCode(ResultEnum.FORBIDDEN.getCode());
      result.setMsg(ResultEnum.FORBIDDEN.getData());
    }
    return result;
  }

  /**
   * 商家账户审核/冻结/更新数据/操作
   */
  @PostMapping("/process")
  public Result processMerchant(@RequestBody Merchant merchant, @RequestHeader("token") String token){
    Result result = new Result();
    try {
      JwtInfo jwtInfo = new JwtInfo();
      if (!(token.isEmpty())) {
        if (!(merchant.getMerchantId() ==null)){
          jwtInfo = jwtRedistService.getUserInfo(token);
          return merchantService.processMerchant(merchant, jwtInfo);
        }else {
          result.setCode(ResultEnum.BADREQUEST.getCode());
          result.setMsg("请求参数不足或不合法！");
        }
      } else {
        jwtInfo.setPass(false);
      }
    } catch (Exception e) {
      result.setCode(ResultEnum.FORBIDDEN.getCode());
      result.setMsg(ResultEnum.FORBIDDEN.getData());
    }
    return result;
  }
}
